package com.blog.controller;

import com.blog.entity.SysFile;
import com.blog.entity.SysUser;

import com.blog.service.Impl.SysFileServiceImpl;
import com.blog.util.file.FileUtils;
import com.blog.util.laydto.LayDto;
import com.blog.util.laydto.LayDtoUtil;
import com.blog.util.uuid.UUIDUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @author cgx
 * @Date 2021/9/23 9:25
 */
@Controller
public class SysFileController {


    @Value("${file.path.headImg}")
    private String headImgPath;

    @Resource
    SysFileServiceImpl fileService;

    /**
     * 上传头像
     * @param
     * @return
     */
    @RequestMapping(value = "/file/upload/headImg")
    @ResponseBody
    public LayDto uploadHeadImg(@RequestParam("file") MultipartFile file, HttpSession session) {

        //获取登录人session
        SysUser loginUser = (SysUser)session.getAttribute("user");
        String loginUserUsername = loginUser.getUsername();
        String loginUserId = loginUser.getId();
        try {
            if (file.isEmpty()) {
                //返回code为1代表失败
                return LayDtoUtil.error(1,"文件为空");
            }
            // 获取大小
            long size = file.getSize();
            // 判断上传文件大小

            if (!FileUtils.checkFileSize(file,10,"M")) {
                return LayDtoUtil.error(1,"文件规定小于10M");
            }
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));

            if (!(".png".equals(suffixName) || ".jpg".equals(suffixName)|| ".jpeg".equals(suffixName))){
                return LayDtoUtil.error(1,"文件类型不对");
            }
            // 设置文件存储路径

            String filePath = headImgPath;
            //将每个人的头像名字改为
            String path = filePath + "headImg-"+loginUserUsername+suffixName;
            File dest = new File(path);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();// 新建文件夹
            }
            //保存文件信息
            SysFile sysFile = new SysFile();
            sysFile.setId(UUIDUtils.getUUId());
            sysFile.setFileText(fileName);
            sysFile.setFilePath(path);
            sysFile.setUploadPeople(loginUserUsername);
            sysFile.setPeopleId(loginUserId);
            sysFile.setFileType("0");
            fileService.save(sysFile);
            // 文件写入
            file.transferTo(dest);

        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return LayDtoUtil.returnSuccess(0);
    }
}
